package ru.arriah.servicedesk.web.commonaction;

import java.io.IOException;
import java.util.Collection;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.RequestBean;
import ru.arriah.servicedesk.bean.TaskBean;
import ru.arriah.servicedesk.ejb.RequestManagerLocalHome;
import ru.arriah.servicedesk.ejb.RequestManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;
import ru.arriah.servicedesk.web.commonaction.exception.PrivilegesException;

public class ProcessRequestReportFormAction extends DumbAction {
  protected RequestManagerLocalHome requestManagerLocalHome;    
  
  
  public ProcessRequestReportFormAction () throws NamingException {
    super();
    
    requestManagerLocalHome = 
      Utils.getRequestManagerHomeInterface();   
  }

  
  public String execute(ActionMapping actionMapping,
      HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    try {     
     
     RequestManagerLocalObject requestManager = 
       requestManagerLocalHome.create();
               
     int requestId = idVal(request.getParameter("request_id"));     
     int userId = Utils.getUserId(request);
     
     String requestReport = request.getParameter("text");
     
     RequestBean requestBean = 
        requestManager.selectRequestDetails(requestId);     
     
     if (!Utils.getUserRole(request).equals("chief"))
        if (requestBean.getLeadExecutorId() == userId) {
           request.setAttribute("status", "request_lead_executor");
        } else
           throw new PrivilegesException("Access denied, request id: " + requestId);
     
     
     if (requestReport == null || requestReport.length() < 1) {
        addErrorMessage(request, config.getProperty("ERROR.REQUIRED_FIELDS"));                          
        Collection<TaskBean> tasksList = 
           requestManager.selectTasksByRequest(requestId);
        
        request.setAttribute("tasksList", tasksList);
        request.setAttribute("requestBean", requestBean);              
        return actionMapping.getFailureTarget();
        
     } else {     
        if (requestManager.setRequestReport(requestId, requestReport)) 
           addInfMessage(
              request, 
              config.getProperty("INF.REQUESTREPORT_ADDED")
           );
        else 
           addErrorMessage(
              request, 
              config.getProperty("ERROR.REQUESTREPORT_NOT_ADDED")
           );
     }                            
     
     Collection<TaskBean> tasksList = 
        requestManager.selectTasksByRequest(requestId);
    
     request.setAttribute("tasksList", tasksList);
     request.setAttribute("requestBean", requestBean);              
     return actionMapping.getSuccessTarget();
     
    } catch (Exception e) {
      handleException(e, "execute");
      throw new InternalException("ErrorMessage.internal");
    }
  }
}